java Arraylist hashmap 安卓
我想将联系人及其号码的HashMap保存到ArrayList中,但我一直有这样的错误“缺少方法体或声明摘要” 私有静态数组列表>;getContacts()
我可以把联系人和他们的号码拿出来,用toast显示出来,但是我需要保存它,然后将它移动到SQLite数据库中
import 安卓.app.Activity;
import 安卓.content.ContentResolver;
import 安卓.content.Intent;
import 安卓.database.Cursor;
import 安卓.net.Uri;
import 安卓.provider.ContactsContract;
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.widget.TextView;
import 安卓.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
public class Contacts extends ActionBarActivity {
private static final int PICK_CONTACT = 1;
//new code
private static ArrayList<HashMap<String, String>> getContacts();
private ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
private HashMap<String, String> contacts = new HashMap<String,String>();
//stop new code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
}
public void btnAddContacts_Click(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
public void btnDone_Click(View view){
Intent i = new Intent(Contacts.this, Message.class);
startActivity(i);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id =
c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone =
c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
null, null);
phones.moveToFirst();
String phn_no = phones.getString(phones.getColumnIndex("data1"));
String name = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DISPLAY_NAME));
//all new code added below
contacts.put(name, phn_no);
while (c.moveToNext()) {
String id1 = c.getString(c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
String name1 = contacts.get(id1);
String phone = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
HashMap<String, String> h = new HashMap<String, String>();
h.put("name", name1);
h.put("phone", phone);
data.add(h);
}
//the toast goes with the old code. it works fine.
Toast.makeText(this, "contact info : " + phn_no + "\n" + name, Toast.LENGTH_LONG).show();
}
}
}
}
}
}
# 1 楼答案
删除
()
——这些用于方法和调用构造函数来初始化对象。如果你想初始化它,也可以使用# 2 楼答案
更改此代码:
致:
你需要它们都是中访问它们的唯一方法
static
。这是在^{编辑
尝试移除
getContacts
,并使其成为这样的getter
现在试试编译